`
https://leetcode.cn/problems/minimize-the-maximum-of-two-arrays/
`

/**
 * @param {number} divisor1
 * @param {number} divisor2
 * @param {number} uniqueCnt1
 * @param {number} uniqueCnt2
 * @return {number}
 */
var minimizeSet = function (divisor1, divisor2, uniqueCnt1, uniqueCnt2) {
  // https://leetcode.cn/problems/minimize-the-maximum-of-two-arrays/solutions/2031827/er-fen-da-an-by-endlesscheng-y8fp/
  const l = lcm(divisor1, divisor2)
  let left = 0, right = (uniqueCnt1 + uniqueCnt2) * 2 - 1

  function check(x) {
    const left1 = Math.max(uniqueCnt1 - Math.floor(x / divisor2) + Math.floor(x / l), 0)
    const left2 = Math.max(uniqueCnt2 - Math.floor(x / divisor1) + Math.floor(x / l), 0)
    const common = x - Math.floor(x / divisor1) - Math.floor(x / divisor2) + Math.floor(x / l)
    return common >= left1 + left2
  }

  while (left < right) {
    const mid = Math.floor((right - left) / 2) + left
    if (check(mid)) {
      right = mid
    } else {
      left = mid + 1
    }
  }

  return left
};

function gcd(a, b) {
  a = Math.abs(a);
  b = Math.abs(b);
  while (b !== 0) {
    [a, b] = [b, a % b];
  }
  return a;
}

function lcm(a, b) {
  return Math.abs(a * b) / gcd(a, b);
}